//
//  DynamicTableView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/2.
//

#import "DynamicTableView.h"

@implementation DynamicTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        self.delegate = self;
        self.dataSource = self;
        self.showsVerticalScrollIndicator = NO;
        [self registerClass:[DynamicTableViewCell class] forCellReuseIdentifier:@"DynamicTableViewCell"];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DynamicTableViewCell" forIndexPath:indexPath];
    cell.frameModel = self.modelArr[indexPath.row];
    @weakify(self)
    [cell setPhotosViewClick:^(DynamicTableViewCell * _Nonnull cell, NSInteger index) {
     @strongify(self)
        if (self.photosViewClick) {
            self.photosViewClick(cell, index);
        }
    }];
    
    [cell setEditTransBlock:^(ShopModel * _Nonnull model) {
        @strongify(self)
        if (self.editTransBlock) {
            self.editTransBlock(model);
        }
    }];
    [cell setTransBlock:^(ShopModel * _Nonnull model) {
        @strongify(self)
        if (self.transBlock) {
            self.transBlock(model);
        }
    }];
    [cell setUpdateGoodsBlock:^(ShopModel * _Nonnull model) {
        @strongify(self)
        if (self.updateGoodsBlock) {
            self.updateGoodsBlock(model);
        }
    }];
    [cell setGoShopBlock:^(ShopModel * _Nonnull model) {
        @strongify(self)
        if (self.goShopBlock) {
            self.goShopBlock(model);
        }
    }];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShopModelFrame *frame = self.modelArr[indexPath.row];
    return frame.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.subject sendNext:indexPath];
}

#pragma mark - 懒加载
- (RACSubject *)subject {
    if (!_subject) {
        _subject = [RACSubject subject];
    }
    return _subject;
}

@end
